#include <iostream>
#include <variant>

int main() {
  // 定义一个std::variant，可以存储int、double或std::string
  std::variant<int, double, std::string> var;

  // 存储一个int值
  var = 42;

  // 使用std::visit来访问variant中的值
  std::visit(
      [](auto&& arg) {
        using T = std::decay_t<decltype(arg)>;
        std::cout << "现在存储的类型是: " << typeid(T).name() << ", 值是: ";
        if constexpr (std::is_same_v<T, int>) {
          std::cout << arg << std::endl;
        } else if constexpr (std::is_same_v<T, double>) {
          std::cout << arg << std::endl;
        } else if constexpr (std::is_same_v<T, std::string>) {
          std::cout << arg << std::endl;
        }
      },
      var);

  // 改变存储的值为double
  var = 3.14;

  std::visit(
      [](auto&& arg) {
        using T = std::decay_t<decltype(arg)>;
        std::cout << "现在存储的类型是: " << typeid(T).name() << ", 值是: ";
        if constexpr (std::is_same_v<T, int>) {
          std::cout << arg << std::endl;
        } else if constexpr (std::is_same_v<T, double>) {
          std::cout << arg << std::endl;
        } else if constexpr (std::is_same_v<T, std::string>) {
          std::cout << arg << std::endl;
        }
      },
      var);

  // 改变存储的值为std::string
  var = "Hello, world!";

  std::visit(
      [](auto&& arg) {
        using T = std::decay_t<decltype(arg)>;
        std::cout << "现在存储的类型是: " << typeid(T).name() << ", 值是: ";
        if constexpr (std::is_same_v<T, int>) {
          std::cout << arg << std::endl;
        } else if constexpr (std::is_same_v<T, double>) {
          std::cout << arg << std::endl;
        } else if constexpr (std::is_same_v<T, std::string>) {
          std::cout << arg << std::endl;
        }
      },
      var);

  return 0;
}